<template>
	<view class="content">
		<view class="list">
			<view class="child flexbetween">
				<view class="name">商品名称</view>
				<input style="width: 500rpx;" type="text" placeholder="请输入商品名称，例如“儿童服装”" v-model="name">
			</view>
			<view class="child flexbetween">
				<view class="name">商品分类</view>
				<view class="flexleft" @click="goodsFlShow=true">
					<input class="margin_right1" type="text" placeholder="请选择商品分类，例如“百货”" disabled v-model="goodsFlName">
					<u-icon name="arrow-right" color="rgba(175, 175, 175, 1)" size="12"></u-icon>
				</view>
			</view>
			<!-- <view class="child flexbetween">
				<view class="name">商品标签</view>
				<view class="flexleft" @click="goodsBqShow=true">
					<input class="margin_right1" type="text" placeholder="请选择商品标签" disabled v-model="goodsBqName">
					<u-icon name="arrow-right" color="rgba(175, 175, 175, 1)" size="12"></u-icon>
				</view>
			</view> -->
			<view class="child flexbetween">
				<view class="name">运费</view>
				<view class="flexleft" @click="checkYunfei">
					<input class="margin_right1" type="text" placeholder="请选择运费模板" disabled v-model="yunfeiName">
					<u-icon name="arrow-right" color="rgba(175, 175, 175, 1)" size="12"></u-icon>
				</view>
			</view>
			<view class="child flexbetween" @click="goAddSpecs">
				<view class="name">商品规格</view>
				<view class="flexleft">
					<input class="margin_right1" type="text" placeholder="请设置商品规格" disabled v-show="spec_type===''">
					<view style="color: #F3271C;" v-show="spec_type!==''">{{spec_type==1?'多规格':"单规格"}}</view>
					<u-icon name="arrow-right" color="rgba(175, 175, 175, 1)" size="12"></u-icon>
				</view>
			</view>
			<view class="child flexbetween">
				<view class="name">商品参数</view>
				<view class="flexleft" @click="goAddGoodsAttr">
					<input class="margin_right1" type="text" placeholder="请设置商品参数" disabled v-show="!param.length">
					<view style="color: #F3271C;" v-show="param.length">已设置</view>
					<u-icon name="arrow-right" color="rgba(175, 175, 175, 1)" size="12"></u-icon>
				</view>
			</view>
			<view class="padding ffffff radius margin_top">
				<view class="">
					<view class="color_san text_x">商品描述</view>
					<view class="flexleft margin_top">
						<textarea name="" id="" cols="10" rows="10" placeholder-class="placeHolder" placeholder="商品描述"
							style="height: 340rpx;font-size: 30rpx;" v-model="description"></textarea>
					</view>
				</view>
			</view>
			<view class="child">
				<view class="name">商品缩略图</view>
				<view class="ffffff radius">
					<view class="flexleft flex_wrap">
						<view class="item_image_box margin_top" v-if="image_arr">
							<view class="item_image radius">
								<image :src="image_arr" mode=""></image>
								<view class="deletimg" @click="deleimage(1,'image_arr')"></view>
							</view>
						</view>
						<!-- 图片盒子 -->
						<view class="item_image_box margin_top" @click="uploadImg(1,'image_arr')"
							v-if="image_arr.length<1">
							<view class="item_image radius">
								<image src="/static/image/home/noimage.png" mode=""></image>
								<!-- <view class="deletimg"></view> -->
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="child">
				<view class="name">商品主图</view>
				<view class="ffffff radius">
					<view class="flexleft flex_wrap">
						<view class="item_image_box margin_top" v-for="(item,index) in images_arr" :key="index">
							<view class="item_image radius">
								<image :src="item" mode=""></image>
								<view class="deletimg" @click="deleimage(index,'images_arr')"></view>
							</view>
						</view>
						<!-- 图片盒子 -->
						<view class="item_image_box margin_top" @click="uploadImg(3,'images_arr')"
							v-if="images_arr.length<3">
							<view class="item_image radius">
								<image src="/static/image/home/noimage.png" mode=""></image>
								<!-- <view class="deletimg"></view> -->
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="child">
				<view class="name">商品详情</view>
				<view class="ffffff radius">
					<view class="flexleft flex_wrap">
						<view class="item_image_box margin_top" v-for="(item,index) in content_images_arr" :key="index">
							<view class="item_image radius">
								<image :src="item" mode=""></image>
								<view class="deletimg" @click="deleimage(index,'content_images_arr')"></view>
							</view>
						</view>
						<!-- 图片盒子 -->
						<view class="item_image_box margin_top" @click="uploadImg(3,'content_images_arr')"
							v-if="content_images_arr.length<3">
							<view class="item_image radius">
								<image src="/static/image/home/noimage.png" mode=""></image>
								<!-- <view class="deletimg"></view> -->
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer ffffff padding">
			<view class="footerButton" @click="submit">提交</view>
		</view>
		<u-picker :show="goodsFlShow" title="商品分类" :columns="goodsFl_arr" keyName="name" @confirm="goodsFlConfirm"
			@cancel="goodsFlShow=false"></u-picker>
		<u-picker :show="goodsBqShow" title="商品标签" :columns="goodsBq_arr" keyName="name" @confirm="goodsBqConfirm"
			@cancel="goodsBqShow=false"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import upFile from "@/utils/upload.js"
	export default {
		data() {
			return {
				name: "", //商品名称
				goods_category_id: "", //分类ID
				goods_tags_ids: "", //商品标签
				price: "", //	价格
				description: "", //描述
				image: "", //封面图
				image_arr: "", //封面图
				images: [], //轮播图
				images_arr: [], //轮播图
				content_images: [], //商品详情
				content_images_arr: [], //商品详情
				param: [], //商品属性
				logistics_id: "", //运费模板
				yunfeiName: "",
				spec_type: "", //规格:1=多规格,0=单规格
				spu: [], //商品属性
				sku: [], //商品规格
				goodsFlShow: false,
				goodsFlName: "",
				goodsFl_arr: [],
				goodsBqShow: false,
				goodsBqName: "",
				goodsBq_arr: [],
				guigeData: {}, //规格的所有数据，用于跳转传参使用
				listid: "",
			}
		},
		onLoad(options) {
			if (options.id) {
				this.listid = options.id
				uni.setNavigationBarTitle({
					title: "编辑商品"
				});
			}
			this.getHomeInit()
		},
		onShow() {
			uni.$off('selectYunfei')
			uni.$on('selectYunfei', data => {
				this.logistics_id = data.id
				this.yunfeiName = data.name
			})
			uni.$off('setshuxing')
			uni.$on('setshuxing', data => {
				this.param = data
			})
			uni.$off('spggmulti')
			uni.$on('spggmulti', data => {
				this.spec_type = data.spec_type;
				this.sku = data.sku;
				this.spu = data.spu;
				// this.guigeData = data;
				this.guigeData = {
					sku: this.sku,
					spu: this.spu,
					spec_type: this.spec_type,
				};
				console.log(data, "选择的商品数据");
			})
		},
		methods: {
			submit() {
				if (this.name == "") {
					httpRequest.toast("请输入商品名称");
					return false
				}
				if (this.goods_category_id == "") {
					httpRequest.toast("请选择商品分类");
					return false
				}
				// if (this.goods_tags_ids == "") {
				// 	httpRequest.toast("请选择商品标签");
				// 	return false
				// }
				if (this.logistics_id == "") {
					httpRequest.toast("请选择可配送区域");
					return false
				}
				if (this.spec_type === "") {
					httpRequest.toast("请设置商品规格");
					return false
				}
				if (!this.param.length) {
					httpRequest.toast("请设置商品属性");
					return false
				}
				if (this.description == "") {
					httpRequest.toast("请输入商品描述");
					return false
				}
				if (this.image == "") {
					httpRequest.toast("请上传商品缩略图");
					return false
				}
				if (!this.images.length) {
					httpRequest.toast("请上传商品主图");
					return false
				}
				if (!this.content_images.length) {
					httpRequest.toast("请上传商品详情图");
					return false
				}
				let spu = [{
						spu:"",
						spuitem:"",
					}]
				let sku = JSON.parse(JSON.stringify(this.sku))
				if(this.spec_type==0){
					sku[0].image = this.image
				}
				let data = {
					id: this.listid,
					name: this.name,
					goods_category_id: this.goods_category_id,
					goods_tags_ids: this.goods_tags_ids,
					description: this.description,
					image: this.image,
					images: this.images.join(),
					content_images: this.content_images.join(),
					param: JSON.stringify(this.param), //商品属性
					logistics_id: this.logistics_id, //运费模板
					spec_type: this.spec_type,
					spu: this.spec_type?this.spu:spu,
					sku: sku,
				}
				if (!this.listid) {
					delete data.id
				}
				httpRequest.request("/api/shop.goods/save", "POST", data).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 500)
					}
				})
			},
			getHomeInit() {
				httpRequest.request("/api/shop.goods/getform", "GET", {
					id: this.listid
				}).then(res => {
					
					this.goodsFl_arr.push(res.data.goods_category)
					this.goodsBq_arr.push(res.data.goods_tags)
					if(!this.listid){
						return false
					}
					let info = res.data.info
					this.name = res.data.info.name;
					this.sku = res.data.info.sku;
					this.spu = res.data.info.spu;
					this.spec_type = res.data.info.spec_type;
					this.guigeData = {
						sku: this.sku,
						spu: this.spu,
						spec_type: this.spec_type,
					};
					this.content_images = info.content_images.split(",")
					this.content_images_arr = info.content_images_arr
					this.image = info.image
					this.image_arr = info.image_text
					this.images = info.images.split(",")
					this.images_arr = info.images_arr
					this.logistics_id = info.logistics_id
					this.yunfeiName = info.logistics.name
					this.goods_category_id = info.goods_category_id
					this.goodsFlName = info.goods_category_id_text
					this.goods_tags_ids = info.goods_tags_ids
					this.goodsBqName = info.goods_tags_ids_text[0]
					this.description = info.description
					this.param = info.param_json
				})
			},
			goodsFlConfirm(e) {
				this.goods_category_id = e.value[0].id
				this.goodsFlName = e.value[0].name
				this.goodsFlShow = false
			},
			goodsBqConfirm(e) {
				this.goods_tags_ids = e.value[0].id
				this.goodsBqName = e.value[0].name
				this.goodsBqShow = false
			},
			goInvoice() {
				uni.navigateTo({
					url: "/pages_mine/invoiceAide"
				})
			},
			goAddSpecs() { //商品规格
				if (this.guigeData.sku) {
					uni.navigateTo({
						url: "/pages_business/addSpecs?msg=" + encodeURIComponent(JSON.stringify(this.guigeData))
					})
				} else {
					uni.navigateTo({
						url: "/pages_business/addSpecs"
					})
				}

			},
			checkYunfei() { //选择运费模板
				uni.navigateTo({
					url: "/pages_business/freightTemplate?type=1"
				})
			},
			goAddGoodsAttr() { //商品属性
				uni.navigateTo({
					url: "/pages_business/addGoodsAttr?msg=" + encodeURIComponent(JSON.stringify(this.param))
				})
			},
			getuserInfo() { //获取个人信息
				let _this = this;
				httpRequest.request('/api/user/index', 'POST', {}).then(res => {
					if (res.code == 1) {
						_this.nickname = res.data.nickname; //昵称
						_this.avatar = res.data.avatar_text; //头像
					}
				})
			},
			uploadImg(length, value) {
				var that = this;
				if (value == "image_arr") {
					upFile.chooseImage({
						count: 1
					}, function(res) {
						that.image = res.url
						that.image_arr = res.fullurl
					})
				} else if (value == "images_arr") {
					let martial_images = that.images
					let martial_images_arr = that.images_arr
					if (martial_images.length >= length) {
						httpRequest.toast('最多上传' + length + '张图片');
						return;
					}
					upFile.chooseImage({
						count: length - martial_images.length
					}, function(res) {
						martial_images_arr.push(res.fullurl)
						martial_images.push(res.url)
						console.log(martial_images_arr, martial_images);
						that.images_arr = martial_images_arr
						that.images = martial_images
					})
				} else if (value == "content_images_arr") {
					let martial_images = that.content_images
					let martial_images_arr = that.content_images_arr
					if (martial_images.length >= length) {
						httpRequest.toast('最多上传' + length + '张图片');
						return;
					}
					upFile.chooseImage({
						count: length - martial_images.length
					}, function(res) {
						martial_images_arr.push(res.fullurl)
						martial_images.push(res.url)
						console.log(martial_images_arr, martial_images);
						that.content_images_arr = martial_images_arr
						that.content_images = martial_images
					})
				}
			},
			deleimage(index, value) {
				var that = this;
				if (value == "image_arr") {
					that.image_arr = ""
					that.image = ""
				} else if (value == "images_arr") {
					that.images_arr.splice(index, 1)
					that.images.splice(index, 1)
				} else if (value == "content_images_arr") {
					that.content_images_arr.splice(index, 1)
					that.content_images.splice(index, 1)
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.item_image_box {
		width: 33.33%;
	}

	.item_image {
		position: relative;
		width: 180rpx;
		height: 180rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	::v-deep.u-safe-area-inset-bottom {
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);

	}

	.pops {
		padding: 0 60rpx 60rpx;

		.title {
			text-align: center;
			font-size: 36rpx;
			color: #000000;
			padding: 40rpx 0;
		}

		.lists {
			.sex {
				text-align: center;
				margin: 20rpx 0;
				color: #999999;
			}

			.sexs {
				color: #FF7510;
				text-align: center;
				margin: 20rpx 0;
			}
		}

		.submits {
			// background-image: linear-gradient(to right, #42CD5A, #55C900);
			background-color: #FF7510;
			border-radius: 40rpx;
			text-align: center;
			padding: 20rpx 0;
			color: #FFFFFF;
			font-size: 32rpx;
			margin: 30rpx 0rpx 0rpx 0rpx;
		}
	}

	.list {
		//margin: 30rpx 20rpx;
		margin-top: 30rpx;
		padding: 20rpx;
		// border-radius: 20rpx;
		background-color: #FAFAFA;
		// background-color: red;
		padding-bottom: 100rpx;

		.child {
			padding: 30rpx;
			background-color: #ffffff;
			margin-bottom: 30rpx;

			.name {
				font-size: 28rpx;
				font-family: PingFang SC-Heavy, PingFang SC;
				font-weight: 800;
				color: #333333;
				white-space: nowrap;
			}

			.data {
				.gerenxxyjt {
					width: 10rpx;
					height: 18rpx;
					margin-left: 10rpx;
					background-size: 100% 100%;
					// background-image: url('/static/image/my/phone.png');
				}

				.xingbie {
					color: #333333;
					font-size: 28rpx;
				}
			}

			input {
				text-align: right;
				color: #333333;
				font-size: 28rpx;
			}
		}
	}


	.btn {
		position: fixed;
		bottom: 50rpx;
		width: 90vw;
		color: #fff;
		background-color: #d48b39;
		padding: 20rpx 0;
		text-align: center;
		border-radius: 44rpx;
		z-index: 100;
		margin: 30rpx 20rpx;
	}

	input {
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}
</style>